package question.solution264;

import question.SolutionTemplate;

import java.util.ArrayList;
import java.util.List;

public class Solution264 implements SolutionTemplate {

    public int nthUglyNumber(int n) {
        if(n <= 6){
            return n;
        }
        List<Integer> arr = new ArrayList<Integer> ();
        arr.add(0);
        arr.add(1);
        int p2 = 1;
        int p3 = 1;
        int p5 = 1;

        int index = 2;
        while(index < n+1){
            int product2 = arr.get(p2) * 2;
            int product3 = arr.get(p3) * 3;
            int product5 = arr.get(p5) * 5;
            int num = getMin(product2, product3, product5);
            if(num == product2){
                p2++;
            }
            if(num == product3){
                p3++;
            }
            if(num == product5){
                p5++;
            }
            arr.add(num);
            index++;
        }
        return arr.get(n);
    }

    private int getMin(int a, int b, int c){
        int min = a;
        if(min > b){
            min = b;
        }
        if(min > c){
            min = c;
        }
        return min;
    }

    @Override
    public void test(){
        System.out.println(nthUglyNumber(10));
    }

}
